package com.jiuzhe.commercial.ui.components;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.bean.HotelstateBean;
import com.jiuzhe.commercial.ui.resourse.OrderDetailsActivity;

import java.util.ArrayList;

public class EQGridCtrl extends LinearLayout
        implements EQCustomListViewListener, OnItemClickListener {

    public final static int SELECT = 0;
    public static final int textColorClick = 0xff0033;
    public static final int BACKGROUNDDeepBlack = 0x7f0d003c;   //0x7f0d003c
    public static final int textColorNormal = 0xffaeaeae;
    public static final int textColorVariationalContent = 0xff666666;
    //	public static int HeaderHeight = 45;
    public int HeaderHeight;
    //	public static int HeaderHeightLast = 90;
    public int HeaderHeightLast;
    public int mWidth;
    public int mHeight;
    EQGirdListViewBase listView;
    private int fixCount;
    private int totalWidth;
    private EQGridAdapter adapter;
    private EQGridCtrlDataSource adapterSource;
    private EQGridItemView headerView;
    private EQGridHeader fixheader;
    private EQGridHeader otherheader;
    private Context context;
    private boolean icClickItem = false;
    private int clickPosition = 0;
    private int clickPositionCurrent = 0;

    private ArrayList<EQGridHeader> contentHeaders;

    public EQGridCtrl(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.HeaderHeight = (int) context.getResources().getDimensionPixelSize(R.dimen.report_header_height);//22.5
        this.HeaderHeightLast = (int) context.getResources().getDimensionPixelSize(R.dimen.report_header_height_last);//45.0
    }

    public EQGridCtrl(Context context, int nWidth, int nHeight) {
        super(context);
        this.context = context;
        this.mWidth = nWidth;
        this.mHeight = nHeight;
        this.HeaderHeight = (int) context.getResources().getDimensionPixelSize(R.dimen.report_header_height);//22.5
        this.HeaderHeightLast = (int) context.getResources().getDimensionPixelSize(R.dimen.report_header_height_last);//45.0
    }

    public static String stringToUnicode(String string) {

        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            String temp = "\\u" + Integer.toHexString(c);
            unicode.append(temp);
        }

        return unicode.toString();
    }

    public static String getMoreTreatedString(String string) {

        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            String temp = "" + c;
            buffer.append(temp);
        }

        return buffer.toString();
    }

    public ArrayList<EQGridHeader> getContentHeaders() {
        return contentHeaders;
    }

    public void setContentHeaders(ArrayList<EQGridHeader> contentHeaders) {
        this.contentHeaders = contentHeaders;
    }

    public String getTreatedString(String name) {
        String valueString = null;
        if (name != null && name.length() > 0) {
            valueString = name.replaceAll("\r|\t|\n|\\s*", "");
            valueString = valueString.replaceAll(" ", "");
            valueString = valueString.replaceAll("　", "");
            valueString = valueString.trim();
        }
        return valueString;
    }

    public EQGridHeader CreateEQGridHeader() {
        return new EQGridHeader();
    }

    public void SetHeight(int nHeight) {
        this.mHeight = nHeight;
    }

    public void initSubView() {
        init(context);
    }

    public void setFixHeader(EQGridHeader header) {
        fixheader = header;
    }

    public void setOtherHeader(EQGridHeader header) {
        otherheader = header;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //setMeasuredDimension(720, 840);
    }

    private void init(Context context) {
        headerView = findViewById(R.id.gridctrl_header);
        int level = Math.max(fixheader.GetLevel(), otherheader.GetLevel());
        int nHeaderHeight = (level - 1) * HeaderHeight + HeaderHeightLast;
        int nCellHeight = context.getResources().getDimensionPixelSize(R.dimen.pull_fresh_height);    //	tail height
        headerView.setBackgroundColor(getResources().getColor(R.color.gray_bg));
        contentHeaders = new ArrayList<EQGridHeader>();
        contentHeaders.clear();
        if (headerView.fixLinearLayout != null && headerView.otherLinearLayout != null) {
            headerView.fixLinearLayout.removeAllViewsInLayout();
            headerView.otherLinearLayout.removeAllViewsInLayout();
        }
        fixheader.Paint(headerView.fixLinearLayout, context, contentHeaders, adapterSource);
        fixCount = contentHeaders.size();
        otherheader.Paint(headerView.otherLinearLayout, context, contentHeaders, adapterSource);
        totalWidth = otherheader.GetWidth();
        listView = findViewById(R.id.gridctrl_list);
        listView.setDividerHeight(0);
        listView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, mHeight - nHeaderHeight + nCellHeight));
        listView.setEQCustomListViewListener(this);
        listView.setListHeader(headerView);
    }

    public void setGridDataSource(EQGridCtrlDataSource dataSource) {
        this.adapterSource = dataSource;
    }

    public void notifyDataSetChanged() {
        if (adapter == null) {
            int nCellHeight = context.getResources().getDimensionPixelSize(R.dimen.tablecontent_unit_height);
            adapter = new EQGridAdapter(adapterSource, context, contentHeaders, fixCount, totalWidth, nCellHeight);
            if (listView != null) {
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(this);
                listView.setSelection(0);
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void stopLoadMore() {
        if (listView != null) {
            listView.stopLoadMore();
        }
    }

    public void setPullLoadEnable(boolean enable) {
        if (listView != null) {
            listView.setPullLoadEnable(enable);
        }
    }

    public void setNoDataReminder() {
        if (listView != null) {
            listView.showNoDataReminderView();
        }
    }

    public void hideEndReminder(){
        if (listView != null){
            listView.setStateEndHide();
        }
    }

    public void moveToNewPosition(int nRow) {
        if (listView != null) {
            listView.setSelection(nRow);
        }
    }

    @Override
    public void onRefresh() {
    }

    @Override
    public void onLoadMore() {
        if (adapterSource != null) {
            adapterSource.notifyLoadMore();
        }
    }

    public void SetListVisible(boolean bVisible) {
        if (listView != null) {
            if (bVisible) {
                listView.setVisibility(View.VISIBLE);
            } else {
                listView.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View child,
                            int itemPosition, long itemId) {
        selectCurrentRow(parent, child, itemPosition, itemId);
        if (adapterSource != null)
            adapterSource.onClickTableContentListener(itemPosition);
    }

    public void selectCurrentRow(AdapterView<?> parent, View child,
                                 int itemPosition, long itemId) {
        icClickItem = true;
        clickPositionCurrent = itemPosition;
        View view = null;
        int firstVisiblePosition = 0;
        if (null != listView) {
            firstVisiblePosition = listView.getFirstVisiblePosition();
        }

        /**
         * 仅需更新两行视图
         */
        if (parent != null) {
            /**
             * 刷新新行视图
             */
            adapter.getView(itemPosition, parent.getChildAt(clickPositionCurrent - firstVisiblePosition), parent);

            /**
             * 老行视图还原
             */
            if (clickPosition != clickPositionCurrent && parent.getChildAt(clickPosition - firstVisiblePosition) != null) {
                parent.getChildAt(clickPosition - firstVisiblePosition).setBackgroundColor(textColorVariationalContent);
                view = parent.getChildAt(clickPosition - firstVisiblePosition);
                if (view instanceof EQGridItemView) {
                    ((EQGridItemView) view).fixLinearLayout.setBackgroundColor(getResources().getColor(R.color.gray_bg));
                }
            }
        }

        clickPosition = clickPositionCurrent;
    }

    public interface EQGridCtrlDataSource {
        public int getCount();

        public HotelstateBean.BodyBean getBodyBean(int nRow, int Col);

        public String getString(int nRow, int Col);

        public String getDataType(int nRow, int Col);

        public String getDataFormat(int nRow, int Col);

        public String getOrderId(int nRow, int Col);

        public int getOrderStatus(int nRow, int Col);

        public String getUserName(int nRow, int Col);

        public String getSkuId(int nRow, int Col);

        public String getSkuName(int nRow, int Col);

        public String getDate(int nRow, int Col);

        public void notifyHeaderTextViewCreate(TextView v);

        public void notifyLoadMore();

        public void onClickTableContentListener(int nRow);
    }

    public class EQGridAdapter extends BaseAdapter {

        int heightThree;
        int heightFour;
        int heightFive;
        String datatype = null;
        String datafmt = null;
        Drawable detailIcon;
        int popupWindowX;
        int popupWindowY;
        private int textSizeBig = 11;                //	blank value	22 px = 11 dp(under ui situation),11 dp = 11 sp
        private EQGridCtrlDataSource dataSource;
        private Context context;
        private ArrayList<EQGridHeader> contentHeaders;
        private int fixCol;
        private int totalWidth;
        @SuppressWarnings("unused")
        private int cellHeight;
        private float x;
        private float y;
        private float startX;
        private float startY;

        public EQGridAdapter(EQGridCtrlDataSource dataSource, Context context, ArrayList<EQGridHeader> contentHeaders,
                             int nFixCol, int nTotalWidth, int nCellHeight) {
            this.dataSource = dataSource;
            this.context = context;
            this.contentHeaders = contentHeaders;
            this.fixCol = nFixCol;
            this.totalWidth = nTotalWidth;
            this.cellHeight = nCellHeight;
        }

        public int getLinesNumber(String string, int lineTextNumbers) {
            int value = 0;
            int length = 0;
            if (string != null) {
                length = string.length();
                if (length > 0 && lineTextNumbers != 0) {
                    if (length % lineTextNumbers == 0) {
                        value = length / lineTextNumbers;
                    } else {
                        value = length / lineTextNumbers + 1;
                    }
                }
            }

            return value;
        }

        public int getCellHeight(String valueString, int width) {
            heightThree = context.getResources().getDimensionPixelSize(R.dimen.tablecontent_unit_height_three);
            heightFour = context.getResources().getDimensionPixelSize(R.dimen.tablecontent_unit_height_four);
            heightFive = context.getResources().getDimensionPixelSize(R.dimen.tablecontent_unit_height_five);
            int nCellHeight = context.getResources().getDimensionPixelSize(R.dimen.tablecontent_unit_height);
            float unitSize = DimenManager.spTpx(context, textSizeBig);
            int lineTextNumbers = (int) ((float) width / unitSize);
            int linesNumber = getLinesNumber(valueString, lineTextNumbers);
            switch (linesNumber) {
                case 1:
                case 2:
                    nCellHeight = context.getResources().getDimensionPixelSize(R.dimen.tablecontent_unit_height);
                    break;
                case 3:
                    nCellHeight = heightThree;
                    break;
                case 4:
                    nCellHeight = heightFour;
                    break;
                case 5:
                    nCellHeight = heightFive;
                    break;
                default:
                    break;
            }

            return nCellHeight;
        }

        @Override
        public int getCount() {
            if (dataSource != null) {
                return dataSource.getCount();
            }
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        public int computeCellHeight(int position) {
            int height = 0;
            if (dataSource != null) {
                for (int i = 0; i < contentHeaders.size(); i++) {
                    if (!(dataSource.getDataType(position, i)).equals("dt_memo")) {
                        String valueString = " " + getTreatedString(dataSource.getString(position, i));
                        int width = GetColWidth(i);
                        int temp = getCellHeight(valueString, width);
                        if (temp > height) {
                            height = temp;
                        }
                    }

                }
            }

            return height;
        }

        @SuppressLint("ResourceAsColor")
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new EQGridItemView(context);
            }
            float textSize = textSizeBig;
            int nFixColCount = fixCol;
            int nOtherColCount = contentHeaders.size() - nFixColCount;
//			int nHight = computeCellHeight(position);
//            int nHight = 100;
            int nHight = getResources().getDimensionPixelOffset(R.dimen.tablecontent_unit_height);

            EQGridItemView ItemView = null;
            if (convertView instanceof EQGridItemView) {
                ItemView = (EQGridItemView) convertView;
            }
            if (ItemView != null) {
                ItemView.setTotalWidth(totalWidth);
                ItemView.fixLinearLayout.removeAllViews();
                ItemView.otherLinearLayout.removeAllViews();
                ItemView.fixLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                ItemView.otherLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                ItemView.fixLinearLayout.setGravity(Gravity.CENTER_VERTICAL);
                ItemView.otherLinearLayout.setGravity(Gravity.CENTER_VERTICAL);

                if (icClickItem && clickPositionCurrent == position) {
                    ItemView.setBackgroundColor(textColorClick);
                    ItemView.fixLinearLayout.setBackgroundColor(textColorClick);
                } else {
                    ItemView.setBackgroundColor(textColorVariationalContent);
                    // ItemView.fixLinearLayout.setBackgroundColor(R.color.color_title);
                }
            }

            if (dataSource != null) {
                for (int i = 0; i < nFixColCount; i++) {
                    String valueString = dataSource.getString(position, i);
                    EQGridTextView view = new EQGridTextView(context);
                    LayoutParams layoutParams = new LayoutParams(GetColWidth(i), nHight);
                    view.setLayoutParams(layoutParams);
                    view.setText(valueString);
                    view.setTextSize(textSize);
                    view.setPadding(2, 0, 0, 0);
                    view.setGravity(GetColAlign(i) | Gravity.CENTER_VERTICAL | Gravity.CENTER);
//                    if (icClickItem && clickPositionCurrent == position) {
//                        view.setBackgroundColor(textColorClick);
//                    } else {
//                        view.setBackgroundColor(BACKGROUNDDeepBlack);
//                    }
                    view.setBackgroundColor(getResources().getColor(R.color.gray));
                    view.setTextColor(getResources().getColor(R.color.black));

                    LinearLayout container = new LinearLayout(context);
                    container.setOrientation(LinearLayout.VERTICAL);
                    container.setLayoutParams(layoutParams);
                    container.setGravity(Gravity.CENTER);
                    container.addView(view);
                    if (ItemView != null) {
                        ItemView.fixLinearLayout.addView(container);
                    }
                }

                for (int i = 0; i < nOtherColCount; i++) {
                    final int column = i + nFixColCount;
                    String valueString = getTreatedString(dataSource.getString(position, column));
                    EQGridTextView view = new EQGridTextView(context);
                    view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    itemSetBackgroundColor(view, dataSource.getOrderStatus(position, column));
                    if (dataSource.getBodyBean(position, column) != null) {
                                HotelstateBean.BodyBean bodyBean =   dataSource.getBodyBean(position, column);
                             final  String orderId = bodyBean.getOrderId();
                                if (!TextUtils.isEmpty(orderId)){
                    view.setOnClickListener(new OnClickListener() {
                        @Override
                       public void onClick(View v) {
                                Intent intent  = new Intent(getContext(), OrderDetailsActivity.class);
                                intent.putExtra("id",orderId);
                                getContext().startActivity(intent);
                            }
                    });
                    }
                    }

                    view.setPosition(position);
                    view.setColumn(column);
                    LayoutParams layoutParams = new LayoutParams(GetColWidth(column), nHight);
                    view.setLayoutParams(layoutParams);

                    datatype = dataSource.getDataType(position, column);
                    datafmt = dataSource.getDataFormat(position, column);
                    if ("dt_memo".equals(datatype)) {
                        view.setOnTouchListener(new OnTouchListener() {

                            @Override
                            public boolean onTouch(View view, MotionEvent event) {
                                touchFunction(view, event);
                                return true;
                            }
                        });

                    } else {
                        view.setText(valueString);
                    }
                    view.setTextSize(textSize);
                    view.setGravity(GetColAlign(column) | Gravity.CENTER_VERTICAL);
                    view.setTextColor(textColorNormal);

                    LinearLayout container = new LinearLayout(context);
                    container.setOrientation(LinearLayout.VERTICAL);
                    container.setLayoutParams(layoutParams);
                    container.setGravity(Gravity.CENTER);
                    container.addView(view);
                    if (ItemView != null) {
                        ItemView.otherLinearLayout.addView(container);
                    }
                }
            }

            if (ItemView != null && ItemView instanceof EQGirdListViewBase.EQGirdScrollableListItem) {
                // 修改在左右移动列表之后，再进行上下滑动，会导致页面列表项混乱，原因是在新创建item布局的时候，没有进行移动
                if (parent instanceof EQGirdListViewBase) {
                    int itemScrollX = ((EQGirdListViewBase) parent).getItemScrollX();
                    View scorllableView = ((EQGirdListViewBase.EQGirdScrollableListItem) ItemView).getScrollableView();
                    if (itemScrollX != scorllableView.getScrollX()) {
                        int itemScrollY = ItemView.getScrollY();
                        scorllableView.scrollTo(itemScrollX, itemScrollY);
                    }
                }
            }
            if (ItemView != null) {
                convertView = ItemView;
            }

            return convertView;
        }

        private void itemSetBackgroundColor(View view, int status) {
            switch (status) {
                case 1://待支付 白色
                    view.setBackgroundColor(getResources().getColor(R.color.white));
                    break;
                case 2://已支付 浅蓝
                    view.setBackgroundColor(getResources().getColor(R.color.blue));
                    break;
                case 3://已退房 灰色
                    view.setBackgroundColor(getResources().getColor(R.color.gray));
                    break;
                case 4://已过期 白色
                    view.setBackgroundColor(getResources().getColor(R.color.white));
                    break;
                case 5://已取消 白色
                    view.setBackgroundColor(getResources().getColor(R.color.white));
                    break;
                case 6://已入住 红色
                    view.setBackgroundColor(getResources().getColor(R.color.red));
                    break;
                case 7://已完成 白色
                    view.setBackgroundColor(getResources().getColor(R.color.white));
                    break;
                default:
                    view.setBackgroundColor(getResources().getColor(R.color.white));
                    break;
            }
        }

        public void touchFunction(View view, MotionEvent event) {
            x = event.getRawX();
            y = event.getRawY();
            Log.i("EQGridCtrl", "MotionEvent.onTouch:" + x);
            Log.i("EQGridCtrl", "MotionEvent.onTouch:" + y);
            final int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_MOVE:" + MotionEvent.ACTION_MOVE);
                    listView.onTouchEvent(event);
//				view.onTouchEvent(event);
                    break;

                case MotionEvent.ACTION_DOWN:
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_DOWN:" + MotionEvent.ACTION_DOWN);
                    startX = x;
                    startY = y;
//				listView.onTouchEvent(event);
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_DOWN:" + startX);
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_DOWN:" + startY);
                    break;

                case MotionEvent.ACTION_UP:
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_UP:" + MotionEvent.ACTION_UP);
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_UP:" + Math.abs(x - startX));
                    Log.i("EQGridCtrl", "MotionEvent.ACTION_UP:" + Math.abs(y - startY));
                    if (Math.abs(x - startX) < 1.5 && Math.abs(y - startY) < 1.5) {
                    }
                    listView.onTouchEvent(event);
                    if (view instanceof EQGridTextView) {
                        onItemClick(listView, null, ((EQGridTextView) view).getPosition(), 0);
                    }
                    break;
                default:
                    break;
            }
        }

        private int GetColWidth(int nCol) {
            if (contentHeaders != null && contentHeaders.size() > 0) {
                return contentHeaders.get(nCol).GetWidth();
            }
            return 0;
        }

        private int GetColAlign(int nCol) {
            if (contentHeaders != null && contentHeaders.size() > 0) {
                return contentHeaders.get(nCol).GetAlignment();
            }
            return 0;
        }
    }

    public class EQGridHeader {

        public static final int textColorNormal = 0xffaeaeae;            //	blank value
        private int textSize = 11;        //22 in px, 11 in sp or dp
        private float realTextSize;
        private boolean bVirtral;//surround container
        private String name;
        private String id;
        private int width;
        @SuppressWarnings("unused")
        private int height;
        private int alignment;
        private String orderStatus;
        private String orderId;
        private String date;

        private ArrayList<EQGridHeader> subHeaders;

        public EQGridHeader() {
            bVirtral = false;
        }

        public String getOrderStatus() {
            return orderStatus;
        }

        public void setOrderStatus(String orderStatus) {
            this.orderStatus = orderStatus;
        }

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public ArrayList<EQGridHeader> getSubHeaders() {
            return subHeaders;
        }

        public void setSubHeaders(ArrayList<EQGridHeader> subHeaders) {
            this.subHeaders = subHeaders;
        }

        public void SetName(String name) {
            this.name = name;
        }

        public void setAlignment(int nAlignmnet) {
            this.alignment = nAlignmnet;
        }

        public void SetId(String id) {
            this.id = id;
        }

        public void SetWidth(int width) {
            this.width = width;
        }

        public void SetVirtual(boolean bVirtual) {
            this.bVirtral = bVirtual;
        }

        public int GetAlignment() {
            return alignment;
        }

        public int GetWidth() {
            if (width > 0) {
                return width;
            } else if (subHeaders != null && subHeaders.size() > 0) {
                for (EQGridHeader subHeader : subHeaders) {
                    width += subHeader.GetWidth();
                }
            }
            return width;
        }

        public int GetLevel() {
            int nLevel = bVirtral ? 0 : 1;
            int nSubLevel = 0;
            if (subHeaders != null && subHeaders.size() > 0) {
                for (EQGridHeader subHeader : subHeaders) {
                    int nTemp = subHeader.GetLevel();
                    if (nTemp > nSubLevel) {
                        nSubLevel = nTemp;
                    }
                }
            }
            return nLevel + nSubLevel;
        }

        public void AddSubHeader(EQGridHeader header) {
            if (subHeaders == null) {
                subHeaders = new ArrayList<EQGridHeader>();
            }
            subHeaders.add(header);
        }

        public void Paint(ViewGroup parent, Context context, ArrayList<EQGridHeader> contentHeaders, EQGridCtrlDataSource dataSource) {
            realTextSize = textSize;
            if (subHeaders != null && subHeaders.size() > 0) {
                // 所有可视表头的父表头，不需要画
                if (bVirtral) {
                    for (EQGridHeader subHeader : subHeaders) {
                        subHeader.Paint(parent, context, contentHeaders, dataSource);
                    }
                    return;
                }

                int nLevel = GetLevel();
                LinearLayout linearLayoutTotal = new LinearLayout(context);
                linearLayoutTotal.setOrientation(LinearLayout.VERTICAL);
                linearLayoutTotal.setGravity(Gravity.CENTER);
                int nWidth = GetWidth();
                int nHeight = (nLevel - 1) * HeaderHeight + HeaderHeightLast;
                LayoutParams layoutParamsTotal = new LayoutParams(nWidth, nHeight);
                linearLayoutTotal.setLayoutParams(layoutParamsTotal);

                // draw self
                EQGridTextView viewSelf = new EQGridTextView(context);
                LayoutParams layoutParamsSelf = new LayoutParams(nWidth, HeaderHeight);
                viewSelf.setLayoutParams(layoutParamsSelf);
                viewSelf.setText(name);
                viewSelf.setTextColor(getResources().getColor(R.color.gray_bg));
                if (!TextUtils.isEmpty(name) && name.contains("今")) {
                    viewSelf.setTextColor(getResources().getColor(R.color.color_title));
                } else {
                    viewSelf.setTextColor(getResources().getColor(R.color.black));
                }
                viewSelf.setGravity(alignment | Gravity.CENTER_VERTICAL);
                viewSelf.setTextSize(realTextSize);
                //   viewSelf.setTextColor(getResources().getColor(R.color.black));
                viewSelf.setBackgroundColor(getResources().getColor(R.color.gray_bg));
                linearLayoutTotal.addView(viewSelf);

                //draw sub
                LinearLayout linearLayoutSub = new LinearLayout(context);
                linearLayoutSub.setOrientation(LinearLayout.HORIZONTAL);
                LayoutParams layoutParamsSub = new LayoutParams(nWidth, nHeight - HeaderHeight);
                linearLayoutSub.setLayoutParams(layoutParamsSub);
                for (EQGridHeader subHeader : subHeaders) {
                    subHeader.Paint(linearLayoutSub, context, contentHeaders, dataSource);
                }
                ;
                linearLayoutSub.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                linearLayoutTotal.addView(linearLayoutSub);
                parent.addView(linearLayoutTotal);
            } else {
                LinearLayout linearLayoutCurrent = new LinearLayout(context);
                linearLayoutCurrent.setOrientation(LinearLayout.HORIZONTAL);
                LayoutParams layoutParamsCurrent = new LayoutParams(width, HeaderHeightLast);
                linearLayoutCurrent.setLayoutParams(layoutParamsCurrent);
                linearLayoutCurrent.setGravity(Gravity.CENTER);

                EQGridTextView view = new EQGridTextView(context);
                LayoutParams layoutParams = new LayoutParams(width, HeaderHeightLast);
                view.setLayoutParams(layoutParams);
                view.setText(name);

                if (!TextUtils.isEmpty(name) && name.contains("今")) {
                    view.setTextColor(getResources().getColor(R.color.color_title));
                } else {
                    view.setTextColor(getResources().getColor(R.color.black));
                }
                view.setGravity(alignment | Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                view.setTextSize(realTextSize);

                view.setBackgroundColor(getResources().getColor(R.color.gray_bg));
                if (id != null) {
                    view.setTag(id);
                }
                linearLayoutCurrent.addView(view);
                parent.addView(linearLayoutCurrent);
                contentHeaders.add(this);
                if (dataSource != null) {
                    dataSource.notifyHeaderTextViewCreate(view);
                }
            }
        }
    }
}
